package com.itshare.video.common.config.tomcat;

import cn.hutool.core.util.IdUtil;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 限流，降级，熔断，三种方法解决高访问量
 * slf4j 线程日志设置
 * @Author lyr
 * @create 2020/10/12 19:46
 */
@Order(1)
@WebFilter(urlPatterns = "/*")
public class LogTraceFilter implements Filter {
    private static final String TRACE_ID = "_traceId";
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String traceId = servletRequest.getParameter(TRACE_ID);
        if(StringUtils.isEmpty(traceId)) {
            traceId = IdUtil.fastSimpleUUID();

        }


        MDC.put(TRACE_ID,traceId);
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
